package com.hhsjdesign.dynamicui.reddot

import android.graphics.Canvas
import android.graphics.Paint
import android.view.View

class RedDotController {

    private var oX: Int
    private var oY: Int
    private val radius: Int
    private val paint: Paint
    private var show: Boolean = false

    //红点tag
    private val tagList: ArrayList<String> = ArrayList()

    constructor(ox: Int, oy: Int, radius: Int, colorInt: Int) {
        this.oX = ox
        this.oY = oy
        this.radius = radius
        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = colorInt
    }

    fun removeRedDor(view: View) {
        if (!show) return
        show = false
        view.invalidate()
    }

    fun onDrawForeground(canvas: Canvas?) {
        if (show) {
            canvas?.drawCircle(oX.toFloat(), oY.toFloat(), radius.toFloat(), paint)
        }
    }

    fun showRedDor(msgTags: List<String>, view: View) {
        if (show) return
        msgTags.forEach {
            if (tagList.contains(it)) {
                show = true
                view.invalidate()
                return@showRedDor
            }
        }
    }

    fun removeRedDor(msgTags: List<String>, view: View) {
        if (!show) return
        msgTags.forEach {
            if (tagList.contains(it)) {
                show = false
                view.invalidate()
                return@removeRedDor
            }
        }
    }

    var tags: MutableList<String>?
        get() = tagList
        set(value) {
            value?.let {
                tagList.clear()
                tagList.addAll(it)
            }
        }

    fun isShow(): Boolean = show

    /**
     * 设置圆点位置
     * @param oX 圆心x
     * @param oY 圆心y
     * @param view 作用的视图
     */
    fun circle(ox: Int, oY: Int, view: View) {
        this.oX = ox
        this.oY = oY
        view.invalidate()
    }

}